local unpack = GLOBAL.unpack
local KEY = true

local function getColor()
    local state = GLOBAL.TheWorld and GLOBAL.TheWorld.state
    if state.isspring then
        return unpack(GLOBAL.PLAYERCOLOURS.GRASS)
    elseif state.issummer then
        return unpack(GLOBAL.PLAYERCOLOURS.LAVENDER)
    elseif state.isautumn then
        return unpack(GLOBAL.PLAYERCOLOURS.OTHERYELLOW)
    else
        return unpack(GLOBAL.PLAYERCOLOURS.THISTLE)
    end
end

local seasonal_fish = {
    "oceanfish_medium_8",
    "oceanfish_small_6",
    "oceanfish_small_7",
    "oceanfish_small_8",
}
local function addName(inst)
    if not inst.entity then
        return
    end
    local label = inst.entity:AddLabel()
    label:SetFont(GLOBAL.CHATFONT_OUTLINE)
    label:SetFontSize(16)
    label:SetWorldOffset(0, 1, 0)
    if table.contains(seasonal_fish, inst.prefab)then
        label:SetColour(unpack(GLOBAL.PLAYERCOLOURS.CORAL))
    else
        label:SetColour(getColor())
    end
    label:Enable(true)
    if inst.name then
        if KEY then
            label:SetText(inst.name)
        else
            label:SetText("")
        end
    end
end


for i = 1,9 do
    AddPrefabPostInit("oceanfish_medium_"..i, addName)
    AddPrefabPostInit("oceanfish_small_"..i, addName)
end

local Data = "MOD_FISH_NAME"
local function switchKEY()
    KEY = not KEY
    TIP("鱼群显示", "pink", KEY)
    SaveModData(Data, KEY)
    local allfish = PlayerFindEnts(80, {"oceanfishable", "oceanfishinghookable"}, {'INLIMBO', 'player'})
    for _, fish in pairs(allfish) do
        addName(fish)
    end
end

local function OnWorldPostInit(inst)
    inst:ListenForEvent("playeractivated", function ()
        KEY = LoadModData(Data)
        if type(KEY) == "nil" then
            KEY = true
        end
    end)
end
AddPrefabPostInit("world", OnWorldPostInit)



if GetModConfigData("sw_fishname") == "biubiu" then
    DEAR_BTNS:AddDearBtn(GLOBAL.GetInventoryItemAtlas("oceanfish_small_7_inv.tex"), "oceanfish_small_7_inv.tex", "鱼群名字", "显示鱼群名字", true, switchKEY)
else
    AddBindBtn("sw_fishname", switchKEY)
end
